/*
 * redmicro all Copyright (c)
 *  缴存
 * Created by seeker910 on 2015/5/29.
 */

Rsd.define('Rsd.Faith.pages.FiscalPiaoJuXinXiPage', {
    extend: 'Rsd.container.Page',
    requires: [
        'Rsd.Faith.controls.XiaoQuListView',
        'Rsd.control.Grid',
        'Rsd.control.GridToolBar',
        'Rsd.control.PagingBar'],
    xtype:'piaoju-page',
    modelType: 'FiscalPiaoJuXinXiBiaoModel',
    layout:'border',
    items:[
        {
            region: 'west',
            xtype:'list-xiaoqu',
            border:true,
            itemClick:'item_click',
            margin: '0 10 0 0',
            width:300
        },
        {
            xtype:'grid',
            region: 'center',
            label:{
                xtype: 'grid-tool-bar',
                height:60,width:'100%',
                newButtons:[{xtype:'button',text:'新增房屋',width:160,style:{fontSize:'120%'},handler:'btn_new_fangwu'}],
                searchColumns:[
                    {name:'YeZhuXingMing',text:'业主姓名',width:250},
                    {name:'LouDongHao',text:'楼栋',op:'equals',width:250},
                    {name:'DanYuanHao',text:'单元',width:250}]
                ,
                searchHandler:'loadFangWuRows'
            },
            footBar:{xtype:'paging-bar',indexChanged:'pageIndexChanged'},
            columns: [
                {
                    "text": "序号",
                    "xtype": "index",
                    "width": 40,
                    "index": 0
                },
                {
                    "name": "BianHao",
                    "text": "房屋编号",
                    "sortable": true,
                    "index": 0,
                    "width": 100,
                    "nobr": true
                },
                {
                    "name": "YeZhuXingMing",
                    "text": "业主姓名",
                    "sortable": false,
                    "index": 0,
                    "width": 70,
                    "nobr": true
                },

                {
                    "name": "LouDongHao",
                    "text": "楼栋号",
                    "sortable": true,
                    "index": 0,
                    "width": 50,
                    "align":"center",
                    "nobr": true
                },
                {
                    "name": "DanYuanHao",
                    "text": "单元号",
                    "sortable": true,
                    "index": 0,
                    "align":"center",
                    "xtype": "number",
                    "width": 50
                },
                {
                    "name": "ShiHao",
                    "text": "室号",
                    "sortable": true,
                    "index": 0,
                    "align":"center",
                    "width": 50,
                    "nobr": true
                },
                {
                    "name": "FangWuLeiXing",
                    "text": "房屋类型",
                    "sortable": false,
                    "index": 0,
                    "width": 70,
                    "align":"center",
                    "xtype": "enum",
                    "enum": {
                        "name": "Rsd.Faith.Core.FangWuLeiXing",
                        "valueMember": "code",
                        "textMember": "text",
                        "items": [
                            {
                                "code": "DC",
                                "value": 0,
                                "text": "多层"
                            },
                            {
                                "code": "XGC",
                                "value": 1,
                                "text": "小高层"
                            },
                            {
                                "code": "GC",
                                "value": 2,
                                "text": "高层"
                            },
                            {
                                "code": "CGC",
                                "value": 3,
                                "text": "超高层"
                            }
                        ]
                    },
                    "nobr": true
                },
                {
                    "name": "FangWuXingZhi",
                    "text": "房屋性质",
                    "sortable": false,
                    "index": 0,
                    "width": 70,  "xtype": "enum",
                    "enum": {
                        "name": "Rsd.Faith.Core.FangWuXingZhi",
                        "valueMember": "code",
                        "textMember": "text",
                        "items": [
                            {
                                "code": "SP",
                                "value": 0,
                                "text": "商品房"
                            },
                            {
                                "code": "AZ",
                                "value": 1,
                                "text": "安置房"
                            },
                            {
                                "code": "LZ",
                                "value": 2,
                                "text": "廉租房"
                            },
                            {
                                "code": "SY",
                                "value": 3,
                                "text": "商业用房"
                            }
                        ]
                    },
                    "nobr": true
                },
                {
                    "name": "JianZhuMianJi",
                    "text": "建筑面积",
                    "sortable": false,
                    "index": 0,
                    "xtype": "number",
                    "width": 80,
                    "precision": 2
                },
                {
                    "name": "ShouQiJiaoCunJinE",
                    "text": "应缴金额",
                    "sortable": false,
                    "index": 0,
                    "xtype": "number",
                    "width": 100,
                    format:'format_jiao_cun',
                    "precision": 2
                },
                {
                    "name": "BeiYongDiZhi",
                    "text": "地址",
                    "sortable": false,
                    "index": 0,
                    "width": 200,
                    "nobr": true
                },
                {
                    "name": "Remark",
                    "text": "备注",
                    "sortable": false,
                    "index": 200,
                    "width": 50,
                    "nobr": true
                }
            ],
        }

    ],
    fangWuFormFields: [

        {
            "name": "XiaoQuBianHao",
            "label": {
                "text": "小区编号",
                "width": 80
            },
            "index": 0,
            readOnly:true,
            "xtype": "text"
        },
        {
            "name": "LouDongHao",
            "label": {
                "text": "楼栋号",
                "width": 80
            },
            "index": 0,
            textChanged:'build_fang_wu_dai_ma',
            "xtype": "text"
        },

        {
            "name": "DanYuanHao",
            "label": {
                "text": "单元号",
                "width": 80
            },
            "index": 0,
            textChanged:'build_fang_wu_dai_ma',
            "xtype": "number"
        },
        {
            "name": "ShiHao",
            "label": {
                "text": "室号",
                "width": 80
            },
            "index": 0,
            textChanged:'build_fang_wu_dai_ma',
            "xtype": "text"
        },
        {
            "name": "BianHao",
            "label": {
                "text": "编号",
                "width": 80
            },
            "index": 0,
            readOnly:true,
            "xtype": "text"
        },
        {
            "name": "JianZhuMianJi",
            "label": {
                "text": "建筑面积",
                "width": 80
            },
            "index": 0,
            "xtype": "number",
            "format": "0.00"
        },
        {
            "name": "FangWuLeiXing",
            "label": {
                "text": "房屋类型",
                "width": 80
            },
            "index": 0,
            "xtype": "combobox-enum",
            "enum":'Rsd.Faith.Core.FangWuLeiXing'
        },
        {
            "name": "FangWuXingZhi",
            "label": {
                "text": "房屋性质",
                "width": 80
            },
            "index": 0,
            "xtype": "combobox-enum",
            "enum":'Rsd.Faith.Core.FangWuXingZhi'
        },
        {
            "name": "ShouQiJiaoCunJinE",
            "label": {
                "text": "首期缴存金额",
                "width": 80
            },
            "index": 0,
            "xtype": "number",
            "format": "0.00"
        },
        {
            "name": "YeZhuXingMing",
            "label": {
                "text": "业主姓名",
                "width": 80
            },
            "index": 0,
            "xtype": "text"
        },
        {
            "name": "BeiYongDiZhi",
            "label": {
                "text": "备用地址",
                "width": 80
            },
            "index": 0,
            "xtype": "text"
        },
        {
            "name": "Remark",
            "label": {
                "text": "备注",
                "width": 80
            },
            "index": 200,
            "xtype": "text"
        }
    ],
    piaoJuFormFields: [
        {
            "name": "XiaoQuBianHao",
            "label": {
                "text": "小区编号",
                "width": 80
            },
            readOnly:true,
            "xtype": "text"
        },
        {
            "name": "FangWuDaiMa",
            "label": {
                "text": "房屋代码",
                "width": 80
            },
            readOnly:true,
            "xtype": "text"
        },
        {
            "name": "YeZhuXingMing",
            "label": {
                "text": "业主姓名",
                "width": 80
            },
            readOnly:true,
            "xtype": "text"
        },

        {
            "name": "FangWuLeiXing",
            "label": {
                "text": "房屋类型",
                "width": 80
            },
            readOnly:true,
            "xtype": "combobox-enum",
            "enum":'Rsd.Faith.Core.FangWuLeiXing'
        },
        {
            "name": "FangWuXingZhi",
            "label": {
                "text": "房屋性质",
                "width": 80
            },
            readOnly:true,
            "xtype": "combobox-enum",
            "enum":'Rsd.Faith.Core.FangWuXingZhi'
        },
        /* {
             "name": "GouFangZongE",
             "label": {
                 "text": "购房总额",
                 "width": 80
             },
               readOnly:true,
             "xtype": "number",
             "precision": 2
         },*/
        {
            "name": "YingJiaoJinE",
            "label": {
                "text": "应交金额",
                "width": 80
            },
            readOnly:true,
            "xtype": "number",
            "precision": 2
        },
        {
            "name": "ShouFeiBiaoZhun",
            "label": {
                "text": "收费标准",
                "width": 80
            },
            "xtype": "combobox-enum",
            "valueMember": "value",
            "textMember": "text",
            enum:{
                "name": "Rsd.Faith.Core.ShouFeiBiaoZhunZZ",
                "valueMember": "value",
                "textMember": "text",
                "items": [
                    {
                        "code": "Unknown",
                        "value": 0,
                        "text": "未设置"
                    },
                    {
                        "code": "PV002",
                        "value": 2,
                        "text": "房价*2%"
                    },
                    {
                        "code": "PM5",
                        "value": 5,
                        "text": "5元/平方米"
                    },
                    {
                        "code": "PM15",
                        "value": 15,
                        "text": "15元/平方米"
                    },
                    {
                        "code": "PM20",
                        "value": 20,
                        "text": "20元/平方米"
                    },
                    {
                        "code": "PM30",
                        "value": 30,
                        "text": "30元/平方米"
                    },
                    {
                        "code": "PM40",
                        "value": 40,
                        "text": "40元/平方米"
                    }
                ]
            }
        },
        {
            "name": "ShiJiaoJinE",
            "label": {
                "text": "实交金额",
                "width": 80
            },
            required:true,
            "xtype": "number",
            "precision": 2
        },
        {
            "name": "FaPiaoBianHao",
            "label": {
                "text": "发票编号",
                "width": 80
            },
            "xtype": "text"
        },
        {
            "name": "JiaoNaRiQi",
            "label": {
                "text": "开票日期",
                "width": 80
            },
            "xtype": "date",
            "formatString": "yyyy-MM-dd"
        },
        /*  {
              "name": "CaoZuoYuanHao",
              "label": {
                  "text": "操作员号",
                  "width": 80
              },
              "xtype": "text"
          },*/
        {
            "name": "Remark",
            "label": {
                "text": "备注",
                "width": 80
            },
            "xtype": "text"
        }
    ],
    /*
     * */
    constructor: function (config) {
        config = config || {};
        Rsd.apply(this, config);

        var me = this;
        var _grid = this.items[1];
        _grid.dataSource = Rsd.create('Rsd.Faith.stores.BizStore',{modelType:'Rsd.Faith.Models.InfoFangWuXinXiBiaoModel'});
        _grid.openItem= function(record){me.openItem(record);};
    },
    load:function()
    {
        var me = this;
        setTimeout(function () {
            me.items[0].loadData();
        },50);
    },
    item_click:function(item)
    {
        var me = this;

        me.__xiao_qu_bian_hao = item.content.key;
        me.loadFangWuRows();
    },
    /*
    *
    * */
    btn_find:function btn_find() {

        this.items[1].items[0].loadData();
    },
    /*
    * */
    btn_new_and_save:function btn_new_and_save() {
        var  bian_hao = this.__xiao_qu_bian_hao;
        if(Rsd.isEmpty(bian_hao))
        {
            Rsd.alert("请选择小区");
            return;
        }

        var now = new Date();
        var bianHao = (now.getTime()-new Date('1995-01-01').getTime())*10000 + Math.random()*10000;

        //Rsd.create('Rsd.Faith.dialogs.FiscalJiaoCunDialog',{isNew:true,data:{XiaoQuBianHao:this.__xiao_qu_bian_hao,FangWuBianHao:bianHao }}).showDialog();
    },
    /*
    * */
    btn_new_fangwu:function btn_new_fangwu(row)
    {
        var me = this;
        var  bian_hao = this.__xiao_qu_bian_hao;
        if(Rsd.isEmpty(bian_hao))
        {
            Rsd.alert("请选择小区");
            return;
        }

        var _record={XiaoQuBianHao:bian_hao,BianHao:bian_hao+'00000'};
        var _title = '新增房屋信息' +  '[*]';
        var _type = 'Rsd.Faith.Models.InfoFangWuXinXiBiaoModel';

        Rsd.app.showModelView(_type,me.fangWuFormFields,_record,_title,['save','cancel'],false,this);
    },
    /*
    * */
    format_jiao_cun:function btn_jiao_cun(row) {

        var me = this;
        var  btn = Rsd.button('缴存',function () {
            //Rsd.create('Rsd.Faith.dialogs.FiscalJiaoCunDialog',{isNew:false,data:row}).showDialog();

            //debugger;
            var _record={
                XiaoQuBianHao:row['XiaoQuBianHao'],
                FangWuDaiMa:row['BianHao'],
                YeZhuXingMing:row['YeZhuXingMing'],
                FangWuLeiXing:row['FangWuLeiXing'],
                FangWuXingZhi:row['FangWuXingZhi'],
                YingJiaoJinE:row['ShouQiJiaoCunJinE'],
                ShiJiaoJinE:row['ShouQiJiaoCunJinE']
            };
            var _title = ('房屋 ') +  '[房屋代码:' + row['BianHao'] + ']缴存';
            var _type = 'Rsd.Faith.Models.FiscalPiaoJuXinXiBiaoModel';
            Rsd.app.showModelView(_type,me.piaoJuFormFields,_record,_title,['save','cancel'],false,me);
        })
        return [Rsd.newLine(),btn];
    },
    /*
     * */
    loadFangWuRows:function loadFangWuRows(args) {

        var me = this;
        var _args = Rsd.apply({},me.getPagingOpt());
        _args.where= args||[];

        var  bian_hao= me.__xiao_qu_bian_hao;
        if(Rsd.isEmpty(bian_hao))
        {
            Rsd.alert("请选择小区");
            return;
        }
        _args.where.push({name:'xiaoqubianhao',op:'like',value:bian_hao});
        var grid = me.items[1];
        Rsd.showWaiting(this.id,grid);
        delete _args['pageSizeList'];
        grid.loadData(_args,function(data){

            var _data = data.data;
            if(Rsd.isArray(_data))
            {
                me.setPagingOpt({pageSize:_data.length,pageIndex:0,pageCount:1,totalCount:_data.length});
            }
            else
            {
                me.setPagingOpt({pageSize:_data.pagesize,pageIndex:_data.pageindex,pageCount:_data.pagecount,totalCount:_data.total});
            }
            grid.scrollIntoView(true);
            Rsd.closeWaiting(me.id);
        });

    },
    /*
    * */
    build_fang_wu_dai_ma:function build_fang_wu_dai_ma(sender,e)
    {
        if(sender)
        {
            var _form = sender.parent.parent.parent;
            var value = _form.getFieldValue("XiaoQuBianHao") + _form.getFieldValue("LouDongHao") +  _form.getFieldValue("DanYuanHao") + _form.getFieldValue("ShiHao");
            _form.setFieldValue('BianHao',value);
        }
    },

    /*
    * */
    pageIndexChanged: function pageIndexChanged(pagingOpt) {
        //debugger;

        this.loadFangWuRows(null);//只需重新加载，pagingOpt 信息自动获取

    },
    /**
     * @description 获取分页信息,在查询时，自动加上分页信息
     * */
    getPagingOpt:function getPagingOpt() {
        if(this.items[1].footBar instanceof  Rsd.control.PagingBar)
        {
            return this.items[1].footBar.getPaging();
        }
        return {};
    },
    /**
     * @description 设置分页信息,一般在查询条件发发生改变时，需重新设置分页 {pageIndex: 0}
     * */
    setPagingOpt:function setPagingOpt(opt) {

        if(this.items[1].footBar instanceof  Rsd.control.PagingBar)
        {
            return this.items[1].footBar.setPaging(opt);
        }
    },
});